package com.insight.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web MVC配置
 *
 * 配置CORS跨域请求,允许前端应用访问后端API
 *
 * @author Insight Team
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 配置CORS跨域请求
     *
     * 允许的来源:
     * - http://localhost:5173 (Vite默认端口)
     * - http://localhost:5174 (Vite备用端口)
     * - http://localhost:3000 (常用开发端口)
     *
     * 允许的方法: GET, POST, PUT, DELETE, PATCH, OPTIONS
     * 允许的请求头: 所有
     * 允许凭证: 是
     * 最大缓存时间: 3600秒
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedOrigins(
                    "http://localhost:5173",
                    "http://localhost:5174",
                    "http://localhost:3000"
                )
                .allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}
